package JAVA300.第九章容器;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 测试Collection接口的方法
 */

public class TestList {
    public static void main(String[] args) {
        //twoCollection();
        ListTest();



    }
    //单容器方法
    public static void singleCollection(){
        Collection<String> c = new ArrayList<>();
        c.add("hqqq");
        c.add("qyh");
        System.out.println(c);//[hqqq, qyh]
        System.out.println(c.size());

        c.remove("qyh");//仅仅是移除 但是对象还是在的
        System.out.println(c);//[hqqq]

        Object[] objs = c.toArray();
        System.out.println(objs);

        System.out.println(c.contains("qyh"));

        c.clear();
        System.out.println(c);
    }

    //双容器方法
    public static void twoCollection(){
        List<String>  list1 = new ArrayList<>();//List可替换Collection
        list1.add("aa");
        list1.add("bb");
        list1.add("cc");

        List<String> list2 = new ArrayList<>();
        list2.add("aa");
//        list2.add("dd");
//        list2.add("we");

        System.out.println("list1"+list1);
//并集
//        list1.addAll(list2);
//        System.out.println("list1"+list1);
////补集
//        list1.removeAll(list2);
//        System.out.println("list1"+list1);
//交集
        list1.retainAll(list2);
        System.out.println("list1"+list1);//list1[aa]

//a 是不是包含b所有元素，返回true/false
        System.out.println(list1.containsAll(list2));



    }

    //List索引相关方法
    public static void ListTest(){
        List<String>  list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        System.out.println(list);//[a, b, c, d]

        list.add(2,"hqq");
        System.out.println(list);//[a, b, hqq, c, d]

        list.remove(2);
        System.out.println(list);//[a, b, c, d]

        list.set(2,"hqq");
        System.out.println(list);//[a, b, hqq, d]


        System.out.println(list.get(2));//hqq

        System.out.println(list.indexOf("hqq"));//2--首次出现

        list.add("a" );
        list.add("b" );
        System.out.println(list);//[a, b, hqq, d, a, b]
        System.out.println(list.lastIndexOf("b"));//5













    }

}
